package com.ruoyi.disasterassessment.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.disasterassessment.domain.IotWarning;
import com.ruoyi.disasterassessment.dto.ContentDto;
import com.ruoyi.disasterassessment.dto.DisasterEvaluatingReportDto;
import com.ruoyi.disasterassessment.mapper.IotWarningMapper;
import com.ruoyi.disasterassessment.vo.DisasterEvaluatingReportVo;
import com.ruoyi.disasterassessment.vo.ReportAllVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.disasterassessment.mapper.DisasterEvaluatingReportMapper;
import com.ruoyi.disasterassessment.domain.DisasterEvaluatingReport;
import com.ruoyi.disasterassessment.service.IDisasterEvaluatingReportService;

/**
 * 灾害评估报告Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-20
 */
@Service
public class DisasterEvaluatingReportServiceImpl implements IDisasterEvaluatingReportService 
{
    @Autowired
    private DisasterEvaluatingReportMapper disasterEvaluatingReportMapper;
    @Autowired
    private IotWarningMapper iotWarningMapper;

    /**
     * 查询灾害评估报告
     * 
     * @param id 灾害评估报告主键
     * @return 灾害评估报告
     */
    @Override
    public DisasterEvaluatingReport selectDisasterEvaluatingReportById(Long id)
    {
        return disasterEvaluatingReportMapper.selectDisasterEvaluatingReportById(id);
    }

    /**
     * 查询灾害评估报告列表
     * 
     * @param disasterEvaluatingReport 灾害评估报告
     * @return 灾害评估报告
     */
    @Override
    public List<DisasterEvaluatingReport> selectDisasterEvaluatingReportList(DisasterEvaluatingReport disasterEvaluatingReport)
    {
        return disasterEvaluatingReportMapper.selectDisasterEvaluatingReportList(disasterEvaluatingReport);
    }

    /**
     * 新增灾害评估报告
     * 
     * @param disasterEvaluatingReport 灾害评估报告
     * @return 结果
     */
    @Override
    public int insertDisasterEvaluatingReport(DisasterEvaluatingReport disasterEvaluatingReport)
    {
        return disasterEvaluatingReportMapper.insertDisasterEvaluatingReport(disasterEvaluatingReport);
    }

    /**
     * 修改灾害评估报告
     * 
     * @param disasterEvaluatingReport 灾害评估报告
     * @return 结果
     */
    @Override
    public int updateDisasterEvaluatingReport(DisasterEvaluatingReport disasterEvaluatingReport)
    {
        return disasterEvaluatingReportMapper.updateDisasterEvaluatingReport(disasterEvaluatingReport);
    }

    /**
     * 批量删除灾害评估报告
     * 
     * @param ids 需要删除的灾害评估报告主键
     * @return 结果
     */
    @Override
    public int deleteDisasterEvaluatingReportByIds(Long[] ids)
    {
        return disasterEvaluatingReportMapper.deleteDisasterEvaluatingReportByIds(ids);
    }

    /**
     * 删除灾害评估报告信息
     * 
     * @param id 灾害评估报告主键
     * @return 结果
     */
    @Override
    public int deleteDisasterEvaluatingReportById(Long id)
    {
        return disasterEvaluatingReportMapper.deleteDisasterEvaluatingReportById(id);
    }

    /**
     * 查询灾害评估报告列表
     * @param vo
     * @return
     */
    @Override
    public List<DisasterEvaluatingReportDto> getDisasterEvaluatingReportList(DisasterEvaluatingReportVo vo) {
        List<DisasterEvaluatingReport> disasterEvaluatingReportList = disasterEvaluatingReportMapper.getDisasterEvaluatingReportList(vo);
        List<DisasterEvaluatingReportDto> list = disasterEvaluatingReportList.stream().map(disasterEvaluatingReport -> {
            DisasterEvaluatingReportDto disasterEvaluatingReportDto = new DisasterEvaluatingReportDto();
            BeanUtils.copyProperties(disasterEvaluatingReport, disasterEvaluatingReportDto);
            disasterEvaluatingReportDto.setPageNum(vo.getPageNum());
            disasterEvaluatingReportDto.setPageSize(vo.getPageNum());
            return disasterEvaluatingReportDto;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<ContentDto> getLoadById(Long id) {
        DisasterEvaluatingReport disasterEvaluatingReport = disasterEvaluatingReportMapper.selectDisasterEvaluatingReportById(id);
        String reportJsonContent = disasterEvaluatingReport.getReportJsonContent();
        // 将 JSON 字符串解析为 List<ContentDto> 对象
        List<ContentDto> contentDtoList = JSON.parseObject(reportJsonContent,
                new TypeReference<List<ContentDto>>(){}.getType());
        return contentDtoList;
    }

    /**
     * 查询灾害评估报告reportAll
     * @param vo
     * @return
     */
    @Override
    public List<List<ContentDto>> getReportAll(ReportAllVo vo) {
        List<DisasterEvaluatingReport> disasterEvaluatingReportList = disasterEvaluatingReportMapper.getReportDisasterEvaluatingReportList(vo);
        List<List<ContentDto>> list = disasterEvaluatingReportList.stream().map(disasterEvaluatingReport -> {
            String reportJsonContent = disasterEvaluatingReport.getReportJsonContent();
            // 将 JSON 字符串解析为 List<ContentDto> 对象
            List<ContentDto> contentDtoList = JSON.parseObject(reportJsonContent,
                    new TypeReference<List<ContentDto>>() {
                    }.getType());
            return contentDtoList;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 查询灾害评估报告通过faceId
     * @param faceId
     * @return
     */
    @Override
    public List<DisasterEvaluatingReportDto> getListByFaceId(String faceId) {
        List<DisasterEvaluatingReportDto> disasterEvaluatingReportDtoList = disasterEvaluatingReportMapper.getListByFaceId(faceId);
        return disasterEvaluatingReportDtoList;
    }

    /**
     * 查询当天灾害评估报告列表
     * @param vo
     * @return
     */
    @Override
    public List<DisasterEvaluatingReportDto> getListByNew(DisasterEvaluatingReportVo vo) {
        List<DisasterEvaluatingReportDto> disasterEvaluatingReportDtoList = disasterEvaluatingReportMapper.getListByNew(vo);
        return disasterEvaluatingReportDtoList;
    }
}
